package com.usian.admin.controller;

import com.usian.admin.service.AdChannelService;
import com.usian.api.admin.AdChannelControllerApi;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理", tags = "channel", description = "频道管理API")
public class AdChannelController implements AdChannelControllerApi {
    /*
    @Resource和@Autowired区别?

    @Autowired：对象通过接口，Spring会使用jdk的动态代理，jdk的动态代理只能对实现类生成代理对象，而且还可以对实现类做增强(只适用一个接口对应一个实现类)。Spring会按byType的方式寻找接口的实现类，将其注入。
     @Resource按照byName的方式注入，如果通过 byName 的方式匹配不到，再按 byType的方式去匹配。所以上面的引用可以替换为:@Resource(name="UserServiceImpl") 。
     */

    @Autowired
//    @Resource(name = "AdChannelServiceImpl")
    private AdChannelService channelService;

    @PostMapping("/list")
    @Override
    @ApiOperation(value = "查询频道信息", notes = "根据名称分页查询频道列表")
    @ApiImplicitParam(name = "dto", type = "ChannelDto", value = "查询名称")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto) {
        return channelService.findByNameAndPage(dto);
    }

    @PostMapping("/save")
    @Override
    @ApiOperation(value = "新增频道信息", notes = "新增频道信息")
    @ApiImplicitParam(name = "channel", type = "AdChannel", value = "新增频道")
    public ResponseResult save(@RequestBody AdChannel channel) {
        if (channel == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        channel.setCreatedTime(new Date());
        channelService.save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/update")
    @Override
    @ApiOperation(value = "更新频道信息", notes = "更新频道信息")
    @ApiImplicitParam(name = "channel", type = "AdChannel", value = "更新频道")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return channelService.update(adChannel);
    }

    @GetMapping("/del/{id}")
    @Override
    @ApiOperation(value = "删除频道信息", notes = "删除频道信息")
    @ApiImplicitParam(name = "id", type = "Integer", value = "删除频道")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {
        return channelService.deleteById(id);
    }
    @GetMapping("/channels")
    @Override
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

}
